package com.kaili.db.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.pojo.dto.appoint.AppointGoodsAddDTO;
import com.kaili.pojo.dto.appoint.AppointGoodsEditDTO;
import com.kaili.pojo.dto.appoint.EwbAppointAddDTO;
import com.kaili.client.service.ICustomerService;
import com.kaili.client.service.ILoginUserInfoService;
import com.kaili.pojo.dto.appoint.EwbAppointEditDTO;
import com.kaili.pojo.entity.HsOptEwbAppoint;
import com.kaili.pojo.entity.HsOptEwbAppointGoods;
import com.kaili.db.mapper.HsOptEwbAppointMapper;
import com.kaili.db.service.IHsBasicSeqService;
import com.kaili.db.service.IHsOptEwbAppointGoodsService;
import com.kaili.db.service.IHsOptEwbAppointService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.pojo.vo.AppointQueryVO;
import com.kaili.common.exception.BaseExcept;
import com.kaili.utils.DateUtils;
import com.kaili.common.util.SnowflakeIdFactory;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xujinghai
 * @since 2018-05-18
 */
@Service("ewbAppointService")
@Transactional
public class HsOptEwbAppointServiceImpl extends ServiceImpl<HsOptEwbAppointMapper, HsOptEwbAppoint> implements IHsOptEwbAppointService {


    @Autowired
    ILoginUserInfoService loginUserInfoService;

    @Autowired
    IHsOptEwbAppointGoodsService appointGoodsService;

    @Autowired
    ICustomerService customerService;

    @Autowired
    IHsBasicSeqService seqService;


    @Override
    public int addEwbAppoint(EwbAppointAddDTO addBean) throws BaseExcept {
        HsOptEwbAppoint optEwbAppoint = new HsOptEwbAppoint();
        BeanUtils.copyProperties(addBean,optEwbAppoint);

        optEwbAppoint.setCreatedBy(loginUserInfoService.getLoginUserId());
        optEwbAppoint.setCreatedTime(new Date());
        optEwbAppoint.setSiteId(loginUserInfoService.getLoginSiteId().longValue());
        optEwbAppoint.setAppointDate(new Date());
        List<AppointGoodsAddDTO> appointGoodsList =  addBean.getAppointGoodsList();

        List<HsOptEwbAppointGoods> ewbAppointGoodsList =  new ArrayList<HsOptEwbAppointGoods>();
        for (int i = 0; i < appointGoodsList.size(); i++) {
            AppointGoodsAddDTO appointGoods = appointGoodsList.get(i);
            HsOptEwbAppointGoods ewbAppointGoods = new HsOptEwbAppointGoods();
            BeanUtils.copyProperties(appointGoods , ewbAppointGoods);
            ewbAppointGoods.setCreatedTime(DateUtils.getCurrentDate());
            ewbAppointGoods.setAppointNo(addBean.getAppointNo());
            ewbAppointGoods.setGoodsId(String.valueOf(SnowflakeIdFactory.getNextId()));
            ewbAppointGoodsList.add(ewbAppointGoods);
        }
        baseMapper.insert(optEwbAppoint);
        appointGoodsService.insertBatch(ewbAppointGoodsList);
        return 0;
    }


    @Override
    public Page<AppointQueryVO> queryEwbAppointPageList(Page<AppointQueryVO> page, Map search) throws BaseExcept {
        return page.setRecords(baseMapper.queryEwbAppointList(page,search));
    }

    @Override
    public int deleteEwbAppoint(String appointNo) throws BaseExcept {
         baseMapper.deleteByAppointNo(appointNo);
         return  0;
    }

    @Override
    public int editEwbAppoint(EwbAppointEditDTO editBean) throws BaseExcept {
        HsOptEwbAppoint optEwbAppoint = new HsOptEwbAppoint();
        BeanUtils.copyProperties(editBean,optEwbAppoint);
        optEwbAppoint.setTotalWeight(editBean.getTotalWeight());
        optEwbAppoint.setModifiedTime(DateUtils.getCurrentDate());
        optEwbAppoint.setModifiedBy(loginUserInfoService.getLoginUserId());
        baseMapper.updateByAppointNo(optEwbAppoint);

        List<AppointGoodsEditDTO> appointGoodsList =  editBean.getAppointGoodsList();
        if(CollectionUtils.isNotEmpty(appointGoodsList)){
            for (int i = 0; i < appointGoodsList.size(); i++) {
                AppointGoodsEditDTO appointGoods = appointGoodsList.get(i);
                HsOptEwbAppointGoods ewbAppointGoods = new HsOptEwbAppointGoods();
                BeanUtils.copyProperties(appointGoods , ewbAppointGoods);
                ewbAppointGoods.setGoodsId(String.valueOf(appointGoods.getGoodsId()));
                appointGoodsService.updateById(ewbAppointGoods);
            }
        }

        return 0;
    }

    @Override
    public List<AppointQueryVO> queryEwbAppointList(Map search) throws BaseExcept {
        return baseMapper.queryEwbAppointList(search);
    }

    @Override
    public Page<AppointQueryVO> queryUnBillingAppointPageList(Page<AppointQueryVO> page,Map search) throws BaseExcept {
        return page.setRecords(baseMapper.queryUnBillingAppointList(page,search));
    }


}
